package com.pagepub.expo.action.system;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.StrutsStatics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pagepub.expo.entity.AppFunction;
import com.pagepub.expo.entity.AppUser;
import com.pagepub.expo.manager.AppRoleMng;
import com.pagepub.expo.manager.AppUserMng;

public class SysLoginAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
    @Qualifier("appUserMng")
	private AppUserMng appUserMng;
	
	private String loginUserName;

	private String password;

	private String validateCode;

//	public String execute() throws Exception {
//		System.out.println("this is execute");
//		return null;
//	}

	public void validateSyslogin() {
	//  清空Errors中的信息
		this.clearErrorsAndMessages();
		if(null==loginUserName || loginUserName.length()<2 || loginUserName.length()>16){
			this.addActionError("用户名输入错误!");
		}
		if(null==password || password.length()<1 || password.length()>16){
			this.addActionError("密码输入错误!");
		}
//		if(validateCode == null || "".equals(validateCode)){
//			this.addActionError("码证码不能为空!");
//		}else{
//			Object obj = ActionContext.getContext().getSession().get(
//					"sysRand");			
//			String validationCode = (obj != null) ? obj.toString() : "";
//	
//			if (!validationCode.equalsIgnoreCase(validateCode))
//			{					
//				this.addActionError("验证码输入错误!");
//	
//			}
//		}
	}

	/**
	 * 管理员登陆
	 * 
	 * @return
	 * @throws Exception
	 */
	public String syslogin() throws Exception {

		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(StrutsStatics.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext
				.getContext().get(StrutsStatics.HTTP_RESPONSE);
		HttpSession session = request.getSession();
		if (loginUserName != null && password != null) {
			// 管理员请求登陆
			AppUser appUser = appUserMng.findByName(loginUserName);//appRoleMng.findDispatcherByUseName(loginUserName);
			if (appUser!=null) {				
				if (password.equals(appUser.getPassword())) {
					Cookie cookie = new Cookie("loginUserName", URLEncoder.encode(loginUserName, "GBK"));
					cookie.setPath("/");
					cookie.setMaxAge(60 * 60 * 24 * 7);
					response.addCookie(cookie);
					if(appUser.getUserId()!=null){
						List<AppFunction> userMenus=appUserMng.getUserSysMenuByUserId(appUser.getUserId());
						session.setAttribute("userMenus", userMenus);	
					}
					session.setAttribute("appUser", appUser);	
					response.sendRedirect(request.getContextPath()
							+ "/system/main.jsp");
					return null;
				}else{
					request.setAttribute("msg", "用户名密码错误！");
					return INPUT;	
				}

			} else {
				if (session.getAttribute(loginUserName + "spwdError") == null) {
					int errCount = 1;
					session.setAttribute(loginUserName + "spwdError", errCount);
				} else {
					int errCount = Integer.parseInt(String.valueOf(session
							.getAttribute(loginUserName + "spwdError")));
					errCount++;
					if (errCount >= 3) {
						request.setAttribute("exit", "true");
						session.invalidate();
					} else {
						session.setAttribute(loginUserName + "spwdError",
								errCount);
					}
				}
				request.setAttribute("msg", "用户名或密码错误！");
				return INPUT;
			}
		} else {
			// 跳转到登陆页面
			return INPUT;
		}
	}

	public String sysloginout() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(StrutsStatics.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext
				.getContext().get(StrutsStatics.HTTP_RESPONSE);
		HttpSession session = request.getSession();
		session.invalidate();
		return INPUT;
	}

	public String getLoginUserName() {
		return loginUserName;
	}

	public void setLoginUserName(String loginUserName) {
		this.loginUserName = loginUserName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

}
